//
//  XHNetworkTools.swift
//  gu-新浪微博
//
//  Created by xiaohui on 16/6/27.
//  Copyright © 2016年 gusheghui. All rights reserved.
//

import UIKit
import AFNetworking
enum MethodType:String{
    case GET = "GET"
    case POST = "POST"
}

class XHNetworkTools: AFHTTPSessionManager {
//成功和失败的闭包
    typealias SuccessClosure = (response:AnyObject?)->()
    typealias FailureClosure = (error: NSError)->()
    
    static let sharedTools:XHNetworkTools = {
        
        let networkTools = XHNetworkTools()
        networkTools.responseSerializer.acceptableContentTypes?.insert("text/plain")
        return networkTools
    }()
    
    func requst(method:MethodType,urlString:String,params:AnyObject?,success:SuccessClosure,failure:FailureClosure){
        
//        let a = AFHTTPSessionManager()
        
        if method == MethodType.GET{
            
            self.GET(urlString, parameters: params, progress: nil, success: { (_, response) -> Void in
                
                success(response: response)
                
                }, failure: { (_, error) -> Void in
                    failure(error: error)
            })
            
        }else{
            
            self.POST(urlString, parameters: params, progress: nil, success: { (_, response) -> Void in
                success(response: response)
                }, failure: { (_, error) -> Void in
                    success(response: error)
            })
  
        }
    }
    
}

extension XHNetworkTools{
    
    func loadUserAcount(code:String,success:SuccessClosure,failure:FailureClosure){
        let urlStr = "https://api.weibo.com/oauth2/access_token"
        
        let parameters = [
            
            "client_id":APPKEY,
            "client_secret":APPSECRET,
            "grant_type":"authorization_code",
            "code":code,
            "redirect_uri":REDIRECT_URI
            
        ]
        
        let manager = AFHTTPSessionManager()
        manager.responseSerializer.acceptableContentTypes?.insert("content-type: text/plain")
        
        self.requst(.POST, urlString: urlStr, params: parameters, success: success, failure: failure)
     
        
    }
    
    func authLoadUserInfo(userAccout:XHUserAccount,success:SuccessClosure,failure:FailureClosure){
        
        let urlString = "https://api.weibo.com/2/users/show.json"

        let params = [
            "access_token":userAccout.access_token!,
            "uid":userAccout.uid!
        ]
        self.requst(.GET, urlString: urlString, params: params, success: success, failure: failure)
    }
    
    func homeLoadStatusData(sinceId :Int64, maxId:Int64 ,success:SuccessClosure,failure:FailureClosure){
        
        let urlString = "https://api.weibo.com/2/statuses/home_timeline.json"
        //    since_id	false	int64	若指定此参数，则返回ID比since_id大的微博（即比since_id时间晚的微博），默认为0。 下拉刷新
        //    max_id	false	int64	若指定此参数，则返回ID小于或等于max_id的微博，默认为0 上拉加载更多
        
        let params = [
            "access_token":XHUserAccountTools.sharedTools.accessToken!,
            "since_id":"\(sinceId)",
            "max_id":"\(maxId)"
        ]
        
        self.requst(.GET, urlString: urlString, params: params, success: success, failure: failure)
    }
    
    func composeUpdate(status:String,success:SuccessClosure,failure:FailureClosure){
        let urlString = "https://api.weibo.com/2/statuses/update.json"
        let params = [
        
        "access_token":XHUserAccountTools.sharedTools.accessToken!,
        "status":status
        ]
        
        requst(.POST, urlString: urlString, params: params, success: success, failure: failure)
        
    }
    
}



